package bg.btanev.eightyfour.service.impl;

import java.util.List;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.person.ContactInfo;
import bg.btanev.eightyfour.domain.person.Person;
import bg.btanev.eightyfour.domain.person.PersonName;
import bg.btanev.eightyfour.repository.PersonRepository;
import bg.btanev.eightyfour.service.PersonService;

/**
 *
 * @author B.Tanev
 *
 */
public class PersonServiceImpl implements PersonService {
	private PersonRepository personRepository;

	public PersonServiceImpl() {
	}

	public void setPersonRepository(PersonRepository personRepository) {
		this.personRepository = personRepository;
	}

	@Override
	public Person createAndStorePerson(PersonName personName, ContactInfo contactInfo) {
		return personRepository.createAndStorePerson(personName, contactInfo);
	}

	@Override
	public void savePerson(Person person) {
		personRepository.storePerson(person);
	}

	@Override
	public Person updatePersonDetails(Long identifier, PersonName personName, ContactInfo contactInfo) {
		Validate.notNull(identifier);
		Validate.isTrue(identifier.longValue() > 0);

		Person person = personRepository.findByIdentifier(identifier);
		if (person != null) {
			person.setPersonName(personName);
			person.setContactInfo(contactInfo);

			personRepository.storePerson(person);
		}

		return person;
	}

	@Override
	public void deletePerson(Person person) {
		personRepository.removePerson(person);
	}

	@Override
	public Person findByEmail(String email) {
		return personRepository.findByEmail(email);
	}

	@Override
	public List<Person> findByExactName(PersonName name) {
		return personRepository.findByName(name);
	}

	@Override
	public Person findByIdentifier(Long personId) {
		return personRepository.findByIdentifier(personId);
	}
}
